/*
  (C) 2007 - Boston University
  Contact: Michael Ocean 

	Parts of this file were adapted from a CS511 submission

  This work is a part of the larger snBench project.  Information
  can be found at: http://csr.bu.edu/snbench 

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package stepide;

import stepide.IDEStarter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import snobject.*;
import step.Node;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class ValueOption extends javax.swing.JDialog {
	protected JLabel jValueLabel;

	protected JTextField jValue;

	protected JButton jOK;

	protected JTextField jID;

	protected JLabel jIDLabel;

	protected JButton jCancel;

	protected Node node;

	protected JFrame frame;

	public ValueOption(JFrame frame, Node n) {
		super(frame);
		this.node = n;
		this.frame = frame;
		initGUI();
	}

	private void jOKActionPerformed(ActionEvent evt) {
		changeNode();
		
		((IDEStarter) frame).refreshXML();
		frame.repaint();
		dispose();
	}

	private void jCancelActionPerformed(ActionEvent evt) {
		dispose();
	}

	public void changeNode() {
		snObject snObj = node.GetValue();
		String text = jValue.getText();
		// JOptionPane.showConfirmDialog(this, text + jID.getText());

		if (text.compareTo("true") == 0)
			snObj = new snBoolean(true);
		else if (text.compareTo("false") == 0)
			snObj = new snBoolean(false);
		else if (text.toLowerCase().compareTo("nil") == 0)
			snObj = new snNil();
		else if (isDigit(text))
			snObj = new snInteger(new Integer(text));
		else
			snObj = new snString(text);

		node.SetValueForGUI(snObj);
		node.setID(jID.getText());		
	}

	private boolean isDigit(String str) {
		char[] ca = str.toCharArray();
		for (int i = 0; i < ca.length; i++) {
			if (Character.isDigit(ca[i]))
				continue;
			else
				return false;
		}
		return true;
	}

	protected void initGUI() {
		try {
			setTitle("Node Option with snObject value");
			{
				jOK = new JButton();
				getContentPane().add(jOK);
				jOK.setText("OK");
				jOK.setBounds(55, 125, 60, 28);
				jOK.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jOKActionPerformed(evt);
					}
				});
			}
			{
				jCancel = new JButton();
				getContentPane().add(jCancel);
				jCancel.setText("Cancel");
				jCancel.setBounds(170, 125, 70, 28);
				jCancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jCancelActionPerformed(evt);
					}
				});
			}
			{
				jIDLabel = new JLabel();
				getContentPane().add(jIDLabel);
				jIDLabel.setText("ID:");
				jIDLabel.setBounds(63, 68, 63, 28);
			}
			{
				jID = new JTextField();
				getContentPane().add(jID);
				jID.setText(node.GetID());
				jID.setBounds(100, 70, 150, 28);
			}
			{
				getContentPane().setLayout(null);
			}
			setSize(316, 200);
		} catch (Exception e) {
			e.printStackTrace();
		}
		{
			jValueLabel = new JLabel();
			getContentPane().add(jValueLabel);
			jValueLabel.setText("Value:");
			jValueLabel.setBounds(63, 28, 63, 28);
		}
		{
			jValue = new JTextField();
			getContentPane().add(jValue);
			jValue.setText(node.GetValue()
					.getGenericContent().toString());
			jValue.setBounds(100, 28, 154, 28);
		}
	}
}
